package com.ezops.gateway;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;

import com.ezops.dto.RemoteRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Utility class for generating and resolving the message key.
 * 
 * @author vgaurav
 * 
 */
public class MessageFactory {

	private static final String APP_ID = "EZOPS_Dashboard";
	private static final String HEADER_KEY = "HEADER_KEY";

	/**
	 * 
	 * @param request
	 * @return
	 */
	public static Message generateMessage(RemoteRequest request) {
		MessageProperties properties = new MessageProperties();
		properties.setAppId(APP_ID);
		properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
		properties.setMessageId(request.getMessageIdentifierKey());
		properties.setHeader(HEADER_KEY, request.getMessageIdentifierKey());
		// properties.setUserId(userId)
		ObjectMapper mapper = new ObjectMapper();
		String messageBody;
		try {
			messageBody = mapper.writeValueAsString(request);

			Message message = new Message(messageBody.getBytes(), properties);
			return message;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
